
// 定义全局的Map对象，以便存储tabs的位置信息等
var map = new Map();
var loginDG;
var tab_height = 30;
var maptitle = new Maptitle();
//开始加载方法
$(document).ready(function(){
	//设置header的样式切换
	$('ul[class*="dropdown-menu"]').find('li').bind('click',function(){
		var lj = '',bj='';
		if($(this).find('a').attr('data-color')=='#438EB9'){ lj='lan_02.png';bj='1_02.png';}
		if($(this).find('a').attr('data-color')=='#222A2D'){ lj='shenlan_02.png';bj='4_04.png'; }
		if($(this).find('a').attr('data-color')=='#C6487E'){ lj='hong_02.png';bj='2_04.png';}
		if($(this).find('a').attr('data-color')=='#D0D0D0'){ lj='hui_02.png';bj='3_04.png';}
		
		$('#headdiv').css('background','url("/infomssh/resources/images/'+bj+'") repeat-x');
		$('#headdiv').find('img').attr('src','/infomssh/resources/images/'+lj);
	});
	
	//加载代办事项
	/*  $.post('../s/ajax/getTask',function(data){
		  var i_all=0;
		  var task_html = '';
		  $.each(data,function(i){
			  var ss = data[i].memo;
			  if(ss.length>20) ss = ss.substring(0,20)+'...';
			  task_html += '<li>'+
		            '<a href="#">'+
		             '   <div class="clearfix">'+
		             '       <span class="pull-left" title="'+data[i].memo+'">'+ss+'</span>'+
		             '   </div>'+
		           ' </a>'+
		        '</li>';
			  i_all++;
		  });
		  if(i_all==0){
			  task_html = '<li>'+
	            '<a href="#">'+
	             '   <div class="clearfix">'+
	             '       <span class="pull-left" title="">您当前没有需要办理事项！</span>'+
	             '   </div>'+
	           ' </a>'+
	        '</li>';
		  }
		  $('#task_num').html(i_all);
		  $('#task-main').html(task_html);
		  $('#task-tit').append(i_all+"条待办事项！");
	  });*/
	
});

/**
 * 检测到用户已退出，弹出登录页面，重新登录
 * @param url 登录之后执行的url连接
 */
function openLogin(url){
	var clientwidth = document.documentElement.clientWidth;
	var clientheight = document.documentElement.clientHeight;
	var loginwidth = 650;
	var loginheight = 450;
	var loginleft = (clientwidth-loginwidth)/2;
	var logintop = (clientheight-loginheight)/2;
	if(logintop<0) logintop=0;
	
	loginDG = new J.dialog({
		id:'loginDG', 
		cover:true,
		drag:false, 
		resize:false,
		top:logintop, 
		left:loginleft, 
		width:loginwidth,
		height:loginheight,
		title:'',
		maxBtn:false,
		xButton:false,
		btnBar:false,
		iconTitle:false,
		fixed:true,
		page:'../login.jsp?url='+url
	});
	loginDG.ShowDialog();
}

//加载loading
function YSprogressBlockUI_load(){
	 $('#loadingmanage').css("height",document.body.scrollHeight);
	 $('#loadingmanage').css("width",document.body.scrollWidth);
	 $('#loadingmanage1').css("left",(document.body.scrollWidth-180)/2);
	 $('#loadingmanage2').css("left",(document.body.scrollWidth-180)/2);
	 $('#loadingmanage').css('display','');
	 $('#loadingmanage1').css('display','');
}
//保存loading
function YSprogressBlockUI_save(){
	 $('#loadingmanage').css("height",document.body.scrollHeight);
	 $('#loadingmanage').css("width",document.body.scrollWidth);
	 $('#loadingmanage1').css("left",(document.body.scrollWidth-180)/2);
	 $('#loadingmanage2').css("left",(document.body.scrollWidth-180)/2);
	 $('#loadingmanage').css('display','');
	 $('#loadingmanage2').css('display','');
}
//关闭loading
function unblockUI(){
	 $('#loadingmanage').css('display','none');
	 $('#loadingmanage1').css('display','none');
	 $('#loadingmanage2').css('display','none');
}

var i_window=1;
var isopen_window;
//打开自定义大小窗口层
function open_window(){
	 i_window=1;
	 isopen_window = true;
	 $('#loadimpoty').css("height",document.body.scrollHeight);
	 $('#loadimpoty').css("width",document.body.scrollWidth);
	 $('#loadimpoty-context').css("left",(document.body.scrollWidth-550)/2);
	 $('#loadimpoty').css('display','');
	 $('#loadimpoty-context').css('display','');
	 $('#loadimpoty-context').html('<div style="width:100%;padding-top:5px;float:left;"><div style="color:red;float:left;">正在读取excel文档数据，请稍等</div><div style="color:red;float:left;" id="loading_window"></div></div>');
	 loading_window();
}

function loading_window(){
	if(i_window==1){
		$('#loading_window').html('.');
		i_window++;
	}else if(i_window==2){
		$('#loading_window').html('..');
		i_window++;
	}else if(i_window==3){
		$('#loading_window').html('...');
		i_window++;
	}else if(i_window==4){
		$('#loading_window').html('....');
		i_window++;
	}else if(i_window==5){
		$('#loading_window').html('.....');
		i_window++;
	}else if(i_window==6){
		$('#loading_window').html('......');
		i_window=1;
	}
	setTimeout(loading_window,0.5*1000);
}

//关闭自定义大小窗口层
function close_window(){
	 isopen_window = false;
	 $('#loadimpoty').css('display','none');
	 $('#loadimpoty-context').css('display','none');
	 $('#loadimpoty-context').html('');
	 i_window=1;
}

//向自定义大小窗口层添加内容
function adddata_window(type){
	 if(!isopen_window) return false;
	 clearTimeout(loading_window);
	 i_window=1;
	 $('#loading_window').html('');
	 $('#loading_window').attr('id','loading_window'+type);
	 
	 $('#loadimpoty-context').find('div').css('color','green');
	 
	 if(type=='1'){
		 $('#loadimpoty-context').append('<div style="width:100%;padding-top:5px;float:left;"><div style="color:green;float:left;">已完成读取excel文档数据。</div>'+
			 	'</div>'+
			 	'<div style="width:100%;padding-top:5px;float:left;"><div style="color:red;float:left;">正在校验数据，请稍等</div>'+
			 	'<div style="color:red;float:left;" id="loading_window"></div></div>');
		  loading_window();
	 }else if(type=='2'){
		 $('#loadimpoty-context').append('<div style="width:100%;padding-top:5px;float:left;"><div style="color:green;float:left;">数据校验完成。</div>'+
			 	'</div>'+
			 	'<div style="width:100%;padding-top:5px;float:left;"><div style="color:red;float:left;">正在将数据导入系统，请稍等</div>'+
			 	'<div style="color:red;float:left;" id="loading_window"></div></div>');
		  loading_window();
	 }else if(type=='3'){
		 $('#loadimpoty-context').append('<div style="width:100%;padding-top:5px;float:left;"><div style="color:red;float:left;">数据导入完成。</div>'+
			 	'</div>');
		 setTimeout(close_window,2*1000);
	 }else if(type=='4'){
		 $('#loadimpoty-context').append('<div style="width:100%;padding-top:5px;float:left;"><div style="color:red;float:left;">数据导入失败。</div>'+
		 	'</div>');
	 setTimeout(close_window,2*1000);
}
}
function getClientHeight()
{
    //可见高
    var clientHeight=document.body.clientHeight;//其它浏览器默认值
    if(navigator.userAgent.indexOf("MSIE 6.0")!=-1)
    {
        clientHeight=document.body.clientHeight;
    }
    else if(navigator.userAgent.indexOf("MSIE")!=-1)
    {
        //IE7 IE8
        clientHeight=document.documentElement.offsetHeight
    }
}
function getClientHeight() {
	//可见高
	var clientHeight = document.body.clientHeight;//其它浏览器默认值
	if (navigator.userAgent.indexOf("MSIE 6.0") != -1) {
		clientHeight = document.body.clientHeight;
	} else if (navigator.userAgent.indexOf("MSIE") != -1) {
		//IE7 IE8
		clientHeight = document.documentElement.offsetHeight
	}

	if (navigator.userAgent.indexOf("Chrome") != -1) {
		clientHeight = document.body.scrollHeight;
	}

	if (navigator.userAgent.indexOf("Firefox") != -1) {
		clientHeight = document.documentElement.scrollHeight;
	}
	return clientHeight;
}

//获得网页内容高度
function getContentHeight() {
	//可见高
	var ContentHeight = document.body.scrollHeight;//其它浏览器默认值

	if (navigator.userAgent.indexOf("Chrome") != -1) {
		ContentHeight = document.body.clientHeight;
	}

	if (navigator.userAgent.indexOf("Firefox") != -1) {
		ContentHeight = document.body.offsetHeight;
	}
	return ContentHeight;
}

/**
 * 新加tab
 * @param title tab标签标题
 * @param url 打开内容url地址
 * @param isClosable 是否关闭 ‘1’关闭
 * @param isRefresh 是否率先呢 ‘1’刷新（未使用）
 * @param navig 导航显示内容
 * @param height 页面高度
 * @param menuid 菜单id
 */
function addTab(title, url, isClosable, isRefresh,navig,height,menuid) {
	var closable;
	//settip("");//清空日期提示
	if (isClosable === '1') {
		closable = true;
	}
	if(height==null){
		height = '100px';
	}
	if (map.get(url) != null) {
		$('#contentTabs').tabs('select', map.get(url));
		var tab = $('#contentTabs').tabs('getSelected');
		if($('.toline').css('position')=='fixed'){
			$('#contentTabs').tabs('update', {
				tab: tab,
				options: {
					title: title,
					cloable: closable,
					content: '<iframe scrolling=no src="' + url + '" style="border: 0px; width: 100%; height: '+height+';margin-top: 42px;"/>',
					id: url
				}
			});
		}else{
			$('#contentTabs').tabs('update', {
				tab: tab,
				options: {
					title: title,
					cloable: closable,
					content: '<iframe scrolling=no src="' + url + '" style="border: 0px; width: 100%; height: '+height+';margin-top: 4px;"/>',
					id: url
				}
			});
		}
		map.put(url, $('#contentTabs').tabs('getTabIndex', tab),title);

	} else {
		if(navig==null || navig==''){//说明没传值，用第一级菜单名称加标题
			var index = $('#contentTabs').tabs('getTabIndex', $('#contentTabs').tabs('getSelected'))-1;
			if(index!=-1) navig = maptitle.getnavig(index).split('>>')[0]+'>>'+title;
		}
		
		maptitle.put(title,navig,menuid);
		if($('.toline').css('position')=='fixed'){
			$('#contentTabs').tabs('add', {
				title: title,
				closable: closable,
				content: '<iframe scrolling=no src="' + url + '" style="border: 0px; width: 100%; height: '+height+';margin-top: 42px;"/>',
				id: url
			});
		}else{
			$('#contentTabs').tabs('add', {
				title: title,
				closable: closable,
				content: '<iframe scrolling=no src="' + url + '" style="border: 0px; width: 100%; height: '+height+';margin-top: 4px;"/>',
				id: url
			});
		}
		
	}
	
	$('#iamtip').html('');//将提示框内容置空
	$('.tabs-wrap').find('ul').css('height',tab_height+'px');
	$('.tabs-inner').css({'height':(tab_height-1)+'px','line-height':(tab_height-1)+'px'});
	
}

function closeTab(){
	
	var index = $('#contentTabs').tabs('getTabIndex', $('#contentTabs').tabs('getSelected'))-1;
	$('#contentTabs').tabs('close', maptitle.get(index)); 
}


// 封装Map对象
function Map() {    
	var struct = function(key, value,name) {    
  		this.key = key;    
  		this.value = value;
  		this.name = name;
	}
   
	var put = function(key, value,name){    
		for (var i = 0; i < this.arr.length; i++) {    
			if ( this.arr[i].key === key ) {    
	 			this.arr[i].value = value;  
	 			this.arr[i].name = name;  
				return;    
			}    
		 }    
	     this.arr[this.arr.length] = new struct(key, value,name);    
	}
   
	var get = function(key) {    
	  for (var i = 0; i < this.arr.length; i++) {    
		   if ( this.arr[i].key === key ) {    
		     	return this.arr[i].value;    
		    }    
	   }    
	  return null;    
	}
	
	var getname = function(key) {    
		  for (var i = 0; i < this.arr.length; i++) {    
			   if ( this.arr[i].key === key ) {    
			     	return this.arr[i].name;    
			    }    
		   }    
		  return null;    
		}
   
	var remove = function(key) {    
	  var v;    
	  for (var i = 0; i < this.arr.length; i++) {    
		    v = this.arr.pop();    
		   	if ( v.key === key ) {    
		    	continue;    
		    }    
		   	this.arr.unshift(v);    
	   }    
	}

	var removeAll = function() {
		for (var i = 0; i <= this.arr.length; i++) {
			this.arr.pop();
		}
	}
   
	var size = function() {    
	  	return this.arr.length;    
	}  
   
	var isEmpty = function() {
	  return this.arr.length <= 0;    
	} 
   
	this.arr = new Array();    
	this.get = get;    
	this.put = put;    
	this.remove = remove;    
	this.size = size;    
	this.isEmpty = isEmpty;    
	this.removeAll = removeAll;
	this.getname = getname;
}  

function Maptitle(){
	var struct = function(index,title,tip,navig,menuid) {    
  		this.index = index;    
  		this.title = title;
  		this.tip = tip;//导航中间提示
  		this.navig = navig;//导航
  		this.menuid = menuid;//导航
	}
	
	var put= function(title,navig,menuid){
	     this.arr[this.arr.length] = new struct(this.arr.length,title,'',navig,menuid);   
	}
	
	var puttip= function(index,tip){
		this.arr[index].tip = tip;   
	}
	
	var putnavig= function(index,navig){
		this.arr[index].navig = navig;   
	}
	
	var get = function(index){
		 for (var i = 0; i < this.arr.length; i++) {    
			   if ( this.arr[i].index === index ) {    
			     	return this.arr[i].title;    
			    }    
		   }    
		  return null;  
	}
	
	var gettip = function(index){
		 for (var i = 0; i < this.arr.length; i++) {    
			   if ( this.arr[i].index === index ) {    
			     	return this.arr[i].tip;    
			    }    
		   }    
		  return null;  
	}
	
	var getmenuid = function(index){
		for (var i = 0; i < this.arr.length; i++) {    
			if ( this.arr[i].index === index ) {    
				return this.arr[i].menuid;    
			}    
		}    
		return null;  
	}
	
	var getnavig = function(index){
		for (var i = 0; i < this.arr.length; i++) {    
			if ( this.arr[i].index === index ) {    
				if(this.arr[i].navig!='' && this.arr[i].navig!=null) return this.arr[i].navig;   
				else return this.arr[i].title;   
			}    
		}    
		return null;  
	}
	
	var remove = function(index){
		 for (var i = index-1; i < this.arr.length-1; i++) {    
			 this.arr[i].title =   this.arr[i+1].title;
			 this.arr[i].tip =   this.arr[i+1].tip;
			 this.arr[i].navig =   this.arr[i+1].navig;
		   } 
		 this.arr.pop();
		  return null;  
	}
	
	this.arr = new Array();  
	this.put = put;
	this.get = get;
	this.remove = remove;
	this.getnavig = getnavig;
	this.gettip = gettip;
	this.putnavig = putnavig;
	this.puttip = puttip;
	this.getmenuid = getmenuid;
}

function settip(tip){
	$('#iamtip').html(tip.trim());
	var index = $('#contentTabs').tabs('getTabIndex', $('#contentTabs').tabs('getSelected'))-1;
	maptitle.puttip(index,tip.trim());
}

//返回顶部
var currentPosition,timer;  
function GoTop(){  
    timer=setInterval("runToTop()",1);  
}  
function runToTop(){  
    currentPosition=document.documentElement.scrollTop || document.body.scrollTop;   
    currentPosition-=30;  
    if(currentPosition>0)  
    {  
        window.scrollTo(0,currentPosition);  
    }  
    else  
    {  
        window.scrollTo(0,0);  
        clearInterval(timer);  
    }  
}  

